﻿<UserControl x:Class="Berico.SnagL.UI.Graph"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
             xmlns:snagl_views="clr-namespace:Berico.SnagL.UI"
             xmlns:snagl_controls="clr-namespace:Berico.SnagL.Infrastructure.Controls"
             xmlns:bericoControls="clr-namespace:Berico.Windows.Controls;assembly=Berico.Windows.Controls"
             xmlns:commonUI="clr-namespace:Berico.Common.UI;assembly=Berico.Common"
             xmlns:convertors="clr-namespace:Berico.Common.UI.Converters;assembly=Berico.Common"
             xmlns:behaviors="clr-namespace:Berico.Common.UI.Behaviors;assembly=Berico.Common"
             xmlns:triggers="clr-namespace:Berico.Common.UI.Triggers;assembly=Berico.Common"
             mc:Ignorable="d"
             d:DesignHeight="800" d:DesignWidth="800"
             DataContext="{Binding GraphData, Source={StaticResource Locator}}">

    <UserControl.Resources>
        <convertors:VisibilityConverter x:Key="VisibilityConverter" />
    </UserControl.Resources>
    
    <!-- Attach behaviors -->
    <i:Interaction.Behaviors>
        <!-- Wire the SizeChanged event to the corresponding command in the ViewModel.
             Problems were encountered attempting to do this using a Trigger and
             an EventToCommand object-->
        <behaviors:SizeChangedHandlerBehavior Command="{Binding SizeChangedCommand}" PassEventArgsToCommand="true" />        
        <!-- Wire the custom Double-Click event to the corresponding command in the ViewModel -->
        <behaviors:DoubleClickBehvaior DoubleClickSpeed="500" Command="{Binding MouseDoubleClick}" PassEventArgsToCommand="true" />
    </i:Interaction.Behaviors>

    <!-- Attach Triggers -->
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <triggers:AdvancedEventToCommand Command="{Binding GraphControlLoadedCommand}" PassEventDetailsToCommand="True" />
        </i:EventTrigger>
        <!-- Wire the MouseLeftButtonDown event to the corresponding command in the ViewModel -->
        <i:EventTrigger EventName="MouseLeftButtonDown">
            <cmd:EventToCommand Command="{Binding MouseLeftButtonDown}" PassEventArgsToCommand="True"/>
        </i:EventTrigger>
        <!-- Wire the MouseLeftButtonUp event to the corresponding command in the ViewModel -->
        <i:EventTrigger EventName="MouseLeftButtonUp">
            <cmd:EventToCommand Command="{Binding MouseLeftButtonUp}" PassEventArgsToCommand="True"/>
        </i:EventTrigger>
        <!-- Wire the MouseRightButtonDown event to the corresponding command in the ViewModel -->        
        <i:EventTrigger EventName="MouseRightButtonDown">
            <cmd:EventToCommand Command="{Binding MouseRightButtonDown}" PassEventArgsToCommand="True"/>
        </i:EventTrigger>
        <!-- Wire the MouseRightButtonUp event to the corresponding command in the ViewModel -->        
        <!--<i:EventTrigger EventName="MouseRightButtonUp">
            <cmd:EventToCommand Command="{Binding MouseRightButtonUp}" PassEventArgsToCommand="True"/>
        </i:EventTrigger>-->
        <!-- Wire the MouseWheel event to the corresponding command in the ViewModel -->        
        <!--<i:EventTrigger EventName="MouseWheel">
            <cmd:EventToCommand Command="{Binding MouseWheel}" PassEventArgsToCommand="True"/>
        </i:EventTrigger>-->
        <i:EventTrigger EventName="MouseWheel">
            <triggers:AdvancedEventToCommand Command="{Binding MouseWheel}" PassEventDetailsToCommand="True" />
        </i:EventTrigger>
        <i:EventTrigger EventName="MouseMove">
            <triggers:AdvancedEventToCommand Command="{Binding MouseMove}" PassEventDetailsToCommand="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>

    <Grid x:Name="LayoutRoot">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="KeyDown">
                <cmd:EventToCommand Command="{Binding GraphKeyDownCommand}"  PassEventArgsToCommand="True"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <Grid.Resources>
            <ItemsPanelTemplate x:Key="GraphSurfaceIemsPanelTemplate">
                <!-- This canvas is the main graph canvas where all nodes and edges are drawn -->
                <Canvas x:Name="GraphSurface" Height="{Binding GraphSurfaceHeight}" Width="{Binding GraphSurfaceWidth}" RenderTransformOrigin="0.5,0.5" Background="White">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Loaded">
                            <triggers:AdvancedEventToCommand Command="{Binding GraphSurfaceLoaded}" PassEventDetailsToCommand="True"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Canvas>
            </ItemsPanelTemplate>
        </Grid.Resources>
        <TextBlock Text="NO DATA" Foreground="DarkGray" FontWeight="Bold" FontSize="72"  Visibility="{Binding HasData, Converter={StaticResource VisibilityConverter}, ConverterParameter='true'}" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <Canvas x:Name="GraphViewPort" commonUI:Clip.ToBounds="True" Height="{Binding Height}" Width="{Binding Width}" Background="White">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Loaded">
                    <triggers:AdvancedEventToCommand Command="{Binding GraphViewPortLoaded}" PassEventDetailsToCommand="True"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <!--<Canvas x:Name="GraphViewPort" Height="{Binding Height}" Width="{Binding Width}" Background="Pink">-->
            <Canvas.Clip>
                <RectangleGeometry x:Name="GraphRectangleClip" />
            </Canvas.Clip>
            <!--<internal:GraphItemsControl x:Name="GraphSurfaceIemsControl" ItemsSource="{Binding NodeAndEdgeViewModels}" ItemsPanel="{StaticResource GraphSurfaceIemsPanelTemplate}">-->
            <snagl_controls:GraphItemsControl x:Name="GraphSurfaceIemsControl" ItemsSource="{Binding NodeViewModels, Mode=TwoWay}" ItemsPanel="{StaticResource GraphSurfaceIemsPanelTemplate}" CacheMode="BitmapCache">
                <snagl_controls:GraphItemsControl.ItemTemplate>
                    <DataTemplate>
                        <!-- A content control is used as a place holder.  We need to dynamically determine
                            which DataTemplate to use so we rely on the custom DataTemplateConverter to
                            make this decision for us-->
                        <ContentControl x:Name="Blah" Content="{Binding}" ContentTemplate="{Binding Converter={StaticResource DataTemplateConverter}}" />
                    </DataTemplate>
                </snagl_controls:GraphItemsControl.ItemTemplate>
            </snagl_controls:GraphItemsControl>
            <!--<Rectangle x:Name="AreaSelectionBox" Height="0" Width="0" Fill="Blue" Stroke="Blue" Opacity="0.3" Canvas.Top="0" Canvas.Left="0" Visibility="Collapsed" />-->
        </Canvas>
        <snagl_controls:AttributePopup>
            <i:Interaction.Behaviors>
                <behaviors:AutoPositionPoupBehavior RelativeTarget="{Binding ElementName=LayoutRoot}" />
            </i:Interaction.Behaviors>
        </snagl_controls:AttributePopup>
    </Grid>
</UserControl>

    